home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / lsb_release < prev    next >
Encoding:
Text File  |  2009-06-02  |  11.2 KB  |  341 lines

  1. #!/usr/bin/python
  2.  
  3. # lsb_release command for Debian
  4. # (C) 2005-08 Chris Lawrence <lawrencc@debian.org>
  5.  
  6. #    This package is free software; you can redistribute it and/or modify
  7. #    it under the terms of the GNU General Public License as published by
  8. #    the Free Software Foundation; version 2 dated June, 1991.
  9.  
  10. #    This package is distributed in the hope that it will be useful,
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #    GNU General Public License for more details.
  14.  
  15. #    You should have received a copy of the GNU General Public License
  16. #    along with this package; if not, write to the Free Software
  17. #    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. #    02111-1307, USA.
  19.  
  20. from optparse import OptionParser
  21. import sys
  22. import commands
  23. import os
  24. import re
  25.  
  26. # XXX: Update as needed
  27. # This should really be included in apt-cache policy output... it is already
  28. # in the Release file...
  29. RELEASE_CODENAME_LOOKUP = {
  30.     '1.1' : 'buzz',
  31.     '1.2' : 'rex',
  32.     '1.3' : 'bo',
  33.     '2.0' : 'hamm',
  34.     '2.1' : 'slink',
  35.     '2.2' : 'potato',
  36.     '3.0' : 'woody',
  37.     '3.1' : 'sarge',
  38.     '4.0' : 'etch',
  39.     '5.0' : 'lenny',
  40.     }
  41.  
  42. TESTING_CODENAME = 'lenny'
  43.  
  44. def lookup_codename(release, unknown=None):
  45.     m = re.match(r'(\d+)\.(\d+)(r(\d+))?', release)
  46.     if not m:
  47.         return unknown
  48.  
  49.     shortrelease = '%s.%s' % m.group(1,2)
  50.     return RELEASE_CODENAME_LOOKUP.get(shortrelease, unknown)
  51.  
  52. # LSB compliance packages... may grow eventually
  53. PACKAGES = 'lsb-core lsb-cxx lsb-graphics lsb-desktop lsb-qt4 lsb-languages lsb-multimedia lsb-printing'
  54.  
  55. modnamere = re.compile(r'lsb-(?P<module>[a-z0-9]+)-(?P<arch>[^ ]+)(?: \(= (?P<version>[0-9.]+)\))?')
  56.  
  57. def valid_lsb_versions(version, module):
  58.     # If a module is ever released that only appears in >= version, deal
  59.     # with that here
  60.     if version == '3.0':
  61.         return ['2.0', '3.0']
  62.     elif version == '3.1':
  63.         if module in ('desktop', 'qt4'):
  64.             return ['3.1']
  65.         else:
  66.             return ['2.0', '3.0', '3.1']
  67.     elif version == '3.2':
  68.         if module == 'desktop':
  69.             return ['3.1', '3.2']
  70.         elif module == 'qt4':
  71.             return ['3.1']
  72.         elif module in ('printing', 'languages', 'multimedia'):
  73.             return ['3.2']
  74.         elif module == 'cxx':
  75.             return ['3.0', '3.1', '3.2']
  76.         else:
  77.             return ['2.0', '3.0', '3.1', '3.2']
  78.     elif version == '4.0':
  79.         if module == 'desktop':
  80.             return ['3.1', '3.2', '4.0']
  81.         elif module == 'qt4':
  82.             return ['3.1']
  83.         elif module in ('printing', 'languages', 'multimedia'):
  84.             return ['3.2', '4.0']
  85.         elif module == 'security':
  86.             return ['4.0']
  87.         elif module == 'cxx':
  88.             return ['3.0', '3.1', '3.2', '4.0']
  89.         else:
  90.             return ['2.0', '3.0', '3.1', '3.2', '4.0']
  91.  
  92.     return [version]
  93.  
  94. try:
  95.     set # introduced in 2.4
  96. except NameError:
  97.     import sets
  98.     set = sets.Set
  99.  
  100. # This is Debian-specific at present
  101. def check_modules_installed():
  102.     # Find which LSB modules are installed on this system
  103.     output = commands.getoutput("dpkg-query -f '${Version} ${Provides}\n' -W %s 2>/dev/null" % PACKAGES)
  104.     if not output:
  105.         return []
  106.  
  107.     modules = set()
  108.     for line in output.split(os.linesep):
  109.         version, provides = line.split(' ', 1)
  110.         version = version.split('-', 1)[0]
  111.         for pkg in provides.split(','):
  112.             mob = modnamere.search(pkg)
  113.             if not mob:
  114.                 continue
  115.  
  116.             mgroups = mob.groupdict()
  117.             # If no versioned provides...
  118.             if mgroups.get('version'):
  119.                 module = '%(module)s-%(version)s-%(arch)s' % mgroups
  120.                 modules.add(module)
  121.             else:
  122.                 module = mgroups['module']
  123.                 for v in valid_lsb_versions(version, module):
  124.                     mgroups['version'] = v
  125.                     module = '%(module)s-%(version)s-%(arch)s' % mgroups
  126.                     modules.add(module)
  127.  
  128.     modules = list(modules)
  129.     modules.sort()
  130.     return modules
  131.  
  132. longnames = {'v' : 'version', 'o': 'origin', 'a': 'suite',
  133.              'c' : 'component', 'l': 'label'}
  134.  
  135. def parse_policy_line(data):
  136.     retval = {}
  137.     bits = data.split(',')
  138.     for bit in bits:
  139.         kv = bit.split('=', 1)
  140.         if len(kv) > 1:
  141.             k, v = kv[:2]
  142.             if k in longnames:
  143.                 retval[longnames[k]] = v
  144.     return retval
  145.  
  146. def parse_apt_policy():
  147.     data = []
  148.     
  149.     policy = commands.getoutput('apt-cache policy 2>/dev/null')
  150.     for line in policy.split('\n'):
  151.         line = line.strip()
  152.         m = re.match(r'(\d+)', line)
  153.         if m:
  154.             priority = int(m.group(1))
  155.         if line.startswith('release'):
  156.             bits = line.split(' ', 1)
  157.             if len(bits) > 1:
  158.                 data.append( (priority, parse_policy_line(bits[1])) )
  159.  
  160.     return data
  161.  
  162. def guess_release_from_apt(origin='Debian', component='main',
  163.                            ignoresuites=('experimental'),
  164.                            label='Debian'):
  165.     releases = parse_apt_policy()
  166.  
  167.     if not releases:
  168.         return None
  169.  
  170.     # We only care about the specified origin, component, and label
  171.     releases = [x for x in releases if (
  172.         x[1].get('origin', '') == origin and
  173.         x[1].get('component', '') == component and
  174.         x[1].get('label', '') == label)]
  175.  
  176.     # Check again to make sure we didn't wipe out all of the releases
  177.     if not releases:
  178.         return None
  179.     
  180.     releases.sort()
  181.     releases.reverse()
  182.  
  183.     # We've sorted the list by descending priority, so the first entry should
  184.     # be the "main" release in use on the system
  185.  
  186.     return releases[0][1]
  187.  
  188. def guess_debian_release():
  189.     distinfo = {'ID' : 'Debian'}
  190.  
  191.     kern = os.uname()[0]
  192.     if kern in ('Linux', 'Hurd', 'NetBSD'):
  193.         distinfo['OS'] = 'GNU/'+kern
  194.     elif kern == 'FreeBSD':
  195.         distinfo['OS'] = 'GNU/k'+kern
  196.     else:
  197.         distinfo['OS'] = 'GNU'
  198.  
  199.     distinfo['DESCRIPTION'] = '%(ID)s %(OS)s' % distinfo
  200.  
  201.     if os.path.exists('/etc/debian_version'):
  202.         release = open('/etc/debian_version').read().strip()
  203.         if not release[0:1].isalpha():
  204.             # /etc/debian_version should be numeric
  205.             codename = lookup_codename(release, 'n/a')
  206.             distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
  207.         elif release.endswith('/sid'):
  208.             distinfo['RELEASE'] = 'testing/unstable'
  209.         else:
  210.             distinfo['RELEASE'] = release
  211.  
  212.     # Only use apt information if we did not get the proper information
  213.     # from /etc/debian_version or if we don't have a codename
  214.     # (which will happen if /etc/debian_version does not contain a
  215.     # number but some text like 'testing/unstable' or 'lenny/sid')
  216.     #
  217.     # This is slightly faster and less error prone in case the user
  218.     # has an entry in his /etc/apt/sources.list but has not actually
  219.     # upgraded the system.
  220.     rinfo = guess_release_from_apt()
  221.     if rinfo and not distinfo.get('CODENAME'):
  222.         release = rinfo.get('version')
  223.         if release:
  224.             codename = lookup_codename(release, 'n/a')
  225.         else:
  226.             release = rinfo.get('suite', 'unstable')
  227.             if release == 'testing':
  228.                 # Would be nice if I didn't have to hardcode this.
  229.                 codename = TESTING_CODENAME
  230.             else:
  231.                 codename = 'sid'
  232.         distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
  233.  
  234.     if distinfo.get('RELEASE'):
  235.         distinfo['DESCRIPTION'] += ' %(RELEASE)s' % distinfo
  236.     if distinfo.get('CODENAME'):
  237.         distinfo['DESCRIPTION'] += ' (%(CODENAME)s)' % distinfo
  238.  
  239.     return distinfo
  240.  
  241. # Whatever is guessed above can be overridden in /etc/lsb-release
  242. def get_lsb_information():
  243.     distinfo = {}
  244.     if os.path.exists('/etc/lsb-release'):
  245.         for line in open('/etc/lsb-release'):
  246.             line = line.strip()
  247.             if not line:
  248.                 continue
  249.             # Skip invalid lines
  250.             if not '=' in line:
  251.                 continue
  252.             var, arg = line.split('=', 1)
  253.             if var.startswith('DISTRIB_'):
  254.                 var = var[8:]
  255.                 if arg.startswith('"') and arg.endswith('"'):
  256.                     arg = arg[1:-1]
  257.                 distinfo[var] = arg
  258.     return distinfo
  259.  
  260. def get_distro_information():
  261.     lsbinfo = get_lsb_information()
  262.     # OS is only used inside guess_debian_release anyway
  263.     for key in ('ID', 'RELEASE', 'CODENAME', 'DESCRIPTION',):
  264.         if key not in lsbinfo:
  265.             distinfo = guess_debian_release()
  266.             distinfo.update(lsbinfo)
  267.             return distinfo
  268.     else:
  269.         return lsbinfo
  270.     
  271. def main():
  272.     parser = OptionParser()
  273.     parser.add_option('-v', '--version', dest='version', action='store_true',
  274.                       default=False,
  275.                       help="show LSB modules this system supports")
  276.     parser.add_option('-i', '--id', dest='id', action='store_true',
  277.                       default=False,
  278.                       help="show distributor ID")
  279.     parser.add_option('-d', '--description', dest='description',
  280.                       default=False, action='store_true',
  281.                       help="show description of this distribution")
  282.     parser.add_option('-r', '--release', dest='release',
  283.                       default=False, action='store_true',
  284.                       help="show release number of this distribution")
  285.     parser.add_option('-c', '--codename', dest='codename',
  286.                       default=False, action='store_true',
  287.                       help="show code name of this distribution")
  288.     parser.add_option('-a', '--all', dest='all',
  289.                       default=False, action='store_true',
  290.                       help="show all of the above information")
  291.     parser.add_option('-s', '--short', dest='short',
  292.                       action='store_true', default=False,
  293.                       help="show requested information in short format")
  294.     
  295.     (options, args) = parser.parse_args()
  296.     if args:
  297.         parser.error("No arguments are permitted")
  298.  
  299.     short = (options.short)
  300.     all = (options.all)
  301.     none = not (options.all or options.version or options.id or
  302.                 options.description or options.codename or options.release)
  303.  
  304.     distinfo = get_distro_information()
  305.  
  306.     if none or all or options.version:
  307.         verinfo = check_modules_installed()
  308.         if not verinfo:
  309.             print >> sys.stderr, "No LSB modules are available."
  310.         elif short:
  311.             print ':'.join(verinfo)
  312.         else:
  313.             print 'LSB Version:\t' + ':'.join(verinfo)
  314.  
  315.     if options.id or all:
  316.         if short:
  317.             print distinfo.get('ID', 'n/a')
  318.         else:
  319.             print 'Distributor ID:\t%s' % distinfo.get('ID', 'n/a')
  320.  
  321.     if options.description or all:
  322.         if short:
  323.             print distinfo.get('DESCRIPTION', 'n/a')
  324.         else:
  325.             print 'Description:\t%s' % distinfo.get('DESCRIPTION', 'n/a')
  326.  
  327.     if options.release or all:
  328.         if short:
  329.             print distinfo.get('RELEASE', 'n/a')
  330.         else:
  331.             print 'Release:\t%s' % distinfo.get('RELEASE', 'n/a')
  332.  
  333.     if options.codename or all:
  334.         if short:
  335.             print distinfo.get('CODENAME', 'n/a')
  336.         else:
  337.             print 'Codename:\t%s' % distinfo.get('CODENAME', 'n/a')
  338.  
  339. if __name__ == '__main__':
  340.     main()
  341.